package com.yatube.yygh.admin.controller;

import com.yatube.yygh.admin.service.DictService;
import com.yatube.yygh.common.response.R;
import com.yatube.yygh.model.cmn.Dict;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author Created with IDEA
 * @description --
 * @date 2024-04-23 11:50
 */
@RestController
@RequestMapping("dict")
public class DictController {

    @Resource
    private DictService dictService;

    @Cacheable(value="dict", keyGenerator = "keyGenerator")
    @GetMapping(value="findChildren/{id}")
    public R findChildren(@PathVariable Long id){
        return R.ok(dictService.findChildren(id));
    }

    // 导出excel文档。注意：这里返回对象要为void 否则报错
    @GetMapping("exportData")
    public void exportData(HttpServletResponse httpServletResponse){
        try {
            dictService.exportDictData(httpServletResponse);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @GetMapping("importData")
    public void importData(MultipartFile file){
        try {
            dictService.importDictData(file);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
